﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.Linq;

namespace DevEx
{
    public class CDVT
    {
        private static QLVTDataContext GetDc()
        {
            QLVTDataContext dc = new QLVTDataContext();
            return dc;
        }
                
        public IQueryable<tblDVT> SelectAllRecord()
        {
            QLVTDataContext dc = GetDc();

            var q = from p in dc.tblDVTs
                    orderby p.DVT
                    select p;
            return q;
        }

        public tblDVT SelectRecordByID(int ID_DVT)
        {
            QLVTDataContext dc = GetDc();

            var q = (from p in dc.tblDVTs where p.ID_DVT == ID_DVT select p).SingleOrDefault();
            return q;
        }

        public tblDVT SelectRecordByName(String DVT)
        {
            QLVTDataContext dc = GetDc();

            var q = (from p in dc.tblDVTs where p.DVT.ToUpper() == DVT.ToUpper() select p).SingleOrDefault();
            return q;
        }

        //Insert
        public void Insert(tblDVT obj)
        {
            QLVTDataContext dc = GetDc();
            dc.tblDVTs.InsertOnSubmit(obj);
            dc.SubmitChanges();
        }
        //Delete
        public void Delete(int ID)
        {
            QLVTDataContext dc = GetDc();

            var DVT = dc.tblDVTs.SingleOrDefault(p=>p.ID_DVT == ID);
            dc.tblDVTs.DeleteOnSubmit(DVT);
            dc.SubmitChanges();
        }
        //Update
        public void Update(tblDVT obj)
        {
            QLVTDataContext dc = GetDc();
            dc.tblDVTs.Attach(obj);
            dc.Refresh(RefreshMode.KeepCurrentValues, obj);
            dc.SubmitChanges(ConflictMode.ContinueOnConflict);
        }
    }
}
